Remove all whitespaces from a string

re.sub(r’s+’, ‘’, S)

Remove all whitespaces from a string.
import re

S = ' Python    Exercises '

print("Original string:", S)
print()
print("Without extra spaces:", re.sub(r'\s+', '', S))

Output:

Original string: Python    Exercises

Without extra spaces: PythonExercises